<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
	arr1 = [1, 2, 2, 4, 4, 7];
	arr2 = [];
	// ES6 Set
	function removeDuplicates1(arr){
		return Array.from(new Set(arr));
	}

	// ES6 Map
	function removeDuplicates2(arr){
		const newArr = [];
		let map = new Map();

		for(let i = 0; i < arr.length; i ++){
			if(!map.get(arr[i])){
				newArr.push(arr[i]);
				map.set(arr[i], 1);
			}
		}
		return newArr;
	}

	// sort()
	function removeDuplicates3(arr){
		arr.sort();
		const newArr = [];
		for(let i = 0; i < arr.length; i ++){
			if(arr[i] !== newArr[newArr.length - 1]) newArr.push(arr[i]);
		}

		return newArr;
	}

	// reduce()
	function removeDuplicates4(arr){
		return arr.sort().reduce((pre, cur) => {
			if(pre[pre.length - 1] !== cur){
				pre.push(cur);
			}
			return pre;
		}, []);
	}

//---------------------------------------------------

	// includes()  不太行
	function removeDuplicates5(arr){
		const newArr = [];
		for(let i = 0; i < arr.length; i ++){
			if(!newArr.includes(arr[i])) newArr.push(arr[i]);
		}

		return newArr;
	}


	// forEach + indexOf   不太行
	function removeDuplicates6(arr){
		const newArr = [];
		arr.forEach(item => {
			if(newArr.indexOf(item) === -1) newArr.push(item);
		})
		return newArr;
	}

	// 双重 for  不太行
	function removeDuplicates7(arr){
		const newArr = [];
		let flag;
		for(let i = 0; i < arr.length; i ++){
			flag = false;
			for(let j = i + 1; j < arr.length; j ++){
				if(arr[j] === arr[i]){
					flag = true;
					break;
				}
			}
			if(!flag){
				newArr.push(arr[i]);
			}
		}
		return newArr;
	}


	// filter()  不行
	function removeDuplicates8(arr){
		return arr.filter((item, index) => {
			return arr.indexOf(item) === index;
		});
	}


	// indexOf 和 lastIndexOf // 去重进化版，重复的全部去掉   不太行
	function removeDuplicates9(arr){
		const newArr = [];
		let flag;
		let map = new Map();
		for(let i = 0; i < arr.length; i ++){
			if(arr.indexOf(arr[i]) === arr.lastIndexOf(arr[i])){
				newArr.push(arr[i]);
			}else{
				if(!map.get(arr[i])){
					newArr.push(arr[i]);
					map.set(arr[i], 1);
				}else{
					continue;
				}
			}
		}
		return newArr;
	}


	
	let arr = [];
 
	for(let i = 0; i < 1000000;i ++) {
		arr.push(Math.floor(1000000* Math.random()))
	}

	console.time('test');
	console.log("方法名称")
	removeDuplicates1(arr);
	console.timeEnd('test');

</script>

</body>
</html>